package cn.edu.ctbu.myfirstweb.webapi;

import cn.edu.ctbu.myfirstweb.domain.Student;
import cn.edu.ctbu.myfirstweb.helper.R;
import cn.edu.ctbu.myfirstweb.helper.RUtil;
import cn.edu.ctbu.myfirstweb.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {
    @Autowired
    private StudentService studentService;

    /**
     * 此时读取的url：webapi/student/list
     * @return
     */
    @GetMapping("/list")
    public List<Student> getAll() {
        return studentService.findAll();
    }

    @PostMapping("/insert")
    public Student insert(Student student) {
        return studentService.insert(student);
    }

    @PutMapping("/update")
    public Student update(Student student) {
        Student oldstudent = studentService.getById(student.getId());
        if (StringUtils.isEmpty(student.getPassword())) {
            student.setPassword(oldstudent.getPassword());
        }
        student = studentService.update(student);
        return student;
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Long id) {
        studentService.deleteById(id);
    }

    @GetMapping("/get/{id}")
    public Student get(@PathVariable Long id) {
        Student student = studentService.getById(id);
        student.setPassword("");
        return student;
    }

    @GetMapping("/query/{name}")
    public Student query(@PathVariable String name) {
        return studentService.getByName(name);
    }

    /**
     * 分页获取学生信息
     * @param page
     * @param size
     * @param name
     * @return
     */
    @GetMapping("/getbypage")
    public R<Student> getByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                @RequestParam(value = "size", defaultValue = "3") Integer size,
                                @RequestParam(value = "name", defaultValue = "") String name) {
        /*新版本sort已不可实例化，只能通过sort.by来获取，同时此处的pageale对象已经变为protected的了，现在可通过Pageable.of方法构造*/
        Page<Student> studentPage;
        if (StringUtils.isEmpty(name)) {
            Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "id"));
            studentPage = studentService.findAll(pageable);
        } else {
            Student student = new Student();
            student.setName(name);//创建包含了按例查询字段的实例对象
            Pageable pageable = PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "id"));//设置分页数据
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());//该类用于设置匹配条件
            Example<Student> example = Example.of(student, matcher);//创建example对象，即创建按例查询对象（包含查询字段的实例对象，匹配条件）
            studentPage = studentService.findAll(example, pageable);
        }
        return RUtil.success(studentPage);
    }

}
